import { ref } from 'vue'
import { goDialog, getUUID } from '@/utils'
import { domainid } from '@/utils/cloud/utils'
import cloneDeep from 'lodash/cloneDeep'
import { DialogEnum } from '@/enums/pluginEnum'
import { PageType } from '../../..'
import { getConfigApi, deleteConfigApi, updateConfigApi } from '@/api/cloud/config.js'
import { fetchRouteParams } from '@/utils/router'
import { PageEnum } from '@/enums/pageEnum'
// 数据初始化
export const useDataListInit = () => {
  const list = ref<PageType[]>([])
  const projectInfo = ref<any>({})
  const breadcrumbList = ref<any>([])
  const showModel = ref(false)
  const routeParams: any = fetchRouteParams()
  const activeCardData = ref<PageType>({})

  const getDataList = async () => {
    const projectId = routeParams.id[0]

    // 获取项目数据
    const projectres = await getConfigApi({
      application: 'page_designer',
      name: projectId
    })
    projectInfo.value = {
      ...projectres.msg[0],
      content: JSON.parse(projectres.msg[0].content)
    }
    const nameHash = {
      project: '全部项目',
      template: '我的模板',
      public_project: '定制项目',
      public_template: '公共模板'
    }
    breadcrumbList.value = [
      {
        name: nameHash[projectInfo.value.datasource as keyof typeof nameHash],
        pathName: `${
          projectInfo.value.datasource.includes('project')
            ? PageEnum.BASE_HOME_PROJECT_LIST_NAME
            : PageEnum.BASE_HOME_TEMPLATE_NAME
        }`
      },
      {
        name: projectInfo.value.content.name
      }
    ]

    // 获取项目下页面列表
    const pagesres = await getConfigApi({
      application: 'page_designer',
      datasource: projectId
    })
    list.value = pagesres.msg.map((item: any) => {
      return {
        ...item,
        content: JSON.parse(item.content)
      }
    })
    list.value = list.value.sort((a: PageType, b: PageType) => {
      return (a.content!.seq || 0) - (b.content!.seq || 0)
    })
  }

  // 删除
  const deleteHandle = (cardData: any, index: number) => {
    goDialog({
      type: DialogEnum.DELETE,
      promise: true,
      onPositiveCallback: () => new Promise(res => setTimeout(() => res(1), 1000)),
      promiseResCallback: async (e: any) => {
        await deleteConfigApi({
          id: cardData.id,
          current_flag: 0
        })
        window.$message.success('删除成功')
        list.value.splice(index, 1)
      }
    })
  }

  // 打开模态框
  const openModel = (cardData: PageType) => {
    if (cardData) {
      activeCardData.value = cardData
    } else {
      activeCardData.value = {}
    }
    showModel.value = true
  }

  // 修改
  const editPageHandle = async (val: PageType) => {
    activeCardData.value = cloneDeep(val)
    showModel.value = true
  }

  // 新建
  const createHandle = async (val: PageType) => {
    const id = val.name || getUUID()
    let data: any = {
      app_application: 'page_designer',
      app_datasource: projectInfo.value.name,
      app_name: id,
      domain_id: domainid(),
      name: val.content?.name || '新建页面',
      desc: val.content?.desc,
      in_menu: val.content?.in_menu,
      menu_path: val.content?.menu_path,
      seq: val.content?.seq
    }
    // 修改
    if (val.name) {
      data = {
        ...val.content,
        ...data
      }
    }

    const res = await updateConfigApi(data)
    if (res.code === 200) {
      window.$message.success('操作成功')
      showModel.value = false
      getDataList()
    } else {
      window.$message.error('操作失败')
    }
  }

  return {
    list,
    breadcrumbList,
    showModel,
    activeCardData,
    projectInfo,
    getDataList,
    deleteHandle,
    openModel,
    editPageHandle,
    createHandle
  }
}
